#include <DHT11.h>
#include <delay.h>
#define NUMBER 60
#define SIZE  5
uchar statue;

void delay_10us()
{
	unsigned char i;

	_nop_();
	_nop_();
	_nop_();
	i = 24;
	while (--i);
}

uchar DHT11_ReadByte()
{
	uchar i,count,value=0;
	statue=OK;
	for (i=0;i<8;i++)
	{
		value<<=1;
		count=0;
		while ((count++<NUMBER)&&(Dat==0));
		if (count>=NUMBER)
		{
			statue=ERROR;
			return 0;
		}
		delay_10us();
		delay_10us();
		delay_10us();
		if (Dat!=0)
		{
			value++;
			while ((count++<NUMBER)&&(Dat!=0))
			{
				Dat=1;
			}
		}
		if (count>=NUMBER)
		{
			statue=ERROR;
			return 0;
		}
	}
	return (value);
}

uchar DHT11_ReadStr()
{
	uchar i,add,count=0;
	EA=0;
	Dat=0;
	delay_ms(20);
	Dat=1;
	delay_10us();
	delay_10us();
	delay_10us();//delay_10us();
	if (Dat!=0)
	{
		EA=1;
		return ERROR;
	}
	else
	{
		while ((count++<NUMBER)&&(Dat==0));
		if (count>=NUMBER)
		{
			Dat=1;
			EA=1;
			led=0;
			return ERROR;
		}
		count=0;
		Dat=1;
		while ((count++<NUMBER)&&(Dat!=0));
		if (count>=NUMBER)
		{
			Dat=1;
			EA=1;
			return ERROR;
		}
		for (i=0;i<SIZE;i++)
		{
			dht11_num[i]=DHT11_ReadByte();
			if (statue==ERROR)
			{
				Dat=1;
				EA=1;
				return ERROR;
			}

			if (i!=4)
			{
				add+=dht11_num[i];
			}
		}
		if (add=dht11_num[4])
		{
			Dat=1;
			EA=1;
			return OK;
		}
		else
		{
			EA=1;
			return ERROR;
		}
	}
}
